<template>
	<view class="app-login">

	</view>
</template>

<script>
	import {
		loginApi
	} from '@/api/login.js'
	import {
		navigateBack
	} from '@/scripts/compatible/pageOptions.js'
	import {
		getCode
	} from '@/scripts/compatible/weixinCode.js'

	export default {
		mounted() {
			console.log("app-login");
			this.$emit('setLoading', true)
			this.handleConfirm()
		},
		methods: {
			async handleConfirm() {
				this.$emit('setLoading', true)

				// 1.获取code
				const codeRes = await getCode()
				if (!codeRes) return
				await uni.setStorageSync("code", codeRes.code)

				// 2.请求
				const res = await loginApi()
				console.log(res);
				uni.showToast({
					icon: res ? 'success' : 'error',
					title: res ? (res.data.msg || '登录成功') : '请求失败',
					complete: () => {
						this.$emit('setLoading', true)
						if (res) {
							uni.setStorageSync('userInfo', res.data.data)
							uni.setStorageSync('token', res.data.data.Token.user_token)
							navigateBack()
						} else {
							uni.reLaunch({
								url: '/pages/index/index'
							})
						}
					}
				})
			},
			handleCencel() {
				uni.reLaunch({
					url: '/pages/index/index'
				})
			},
		},
	}
</script>

<style lang="scss" scoped>
	.app-login {}
</style>